package com.solution.admin.order.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.order.entity.OrderLogEntity;
import com.solution.admin.order.service.OrderLogService;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 订单日志流水web控制层
 * @author lilp
 * @date 2017-09-18 19:25:36
 */
@RequestMapping("/orderLog")
@Controller
public class OrderLogController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(OrderLogController.class);

	@Autowired
	private OrderLogService orderLogService;
	

	/**
	 * 查询订单日志流水列表
	 * @param orderLog 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-09-18 19:25:36
	 */
	@RequestMapping("/listOrderLogPage")
	@ResponseBody
	public JSONGrid listOrderLogPage(OrderLogEntity orderLog, HttpServletRequest request){
		
		String orderCode = request.getParameter("orderCode");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderCode", orderCode);
		try {
			List<OrderLogEntity> orderLogList = orderLogService.listOrderLog(paramMap);
			return new JSONGrid(orderLogList,orderLogList.size());
		} catch (Exception e) {
			handleException(" 查询订单日志流水列表", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-09-18 19:25:36
	 */
	@RequestMapping("/toAddOrderLog")
	public String toAddOrderLog(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "order/orderLog_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-09-18 19:25:36
	 */
	@RequestMapping("/toUpdateOrderLog")
	public String toUpdateOrderLog(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			OrderLogEntity orderLog = orderLogService.getOrderLogById(id);
			request.setAttribute("orderLog", orderLog);
		} catch (Exception e) {
			handleException("跳转编辑订单日志流水页面错误", logger, e);
		}
		return "order/orderLog_form";
	}


	/**
	 * 新增或保存订单日志流水
	 * @param request 请求对象
	 * @param orderLogEntity 订单日志流水实体
	 * @return
	 * @author lilp
 	 * @date 2017-09-18 19:25:36
	 */
	@RequestMapping("/saveOrUpdateOrderLog")
	@ResponseBody
	public Map<String, Object> saveOrderLog(HttpServletRequest request, OrderLogEntity orderLogEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isEmpty(id)){
				result = orderLogService.updateOrderLog(orderLogEntity);
			}else{
				result = (int)orderLogService.saveOrderLog(orderLogEntity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加订单日志流水失败", logger, e);
		}
		return resultMap;
	}
	
}
